//
//  OMMallCategoryTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/9.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMMallCategoryTableCell.h"
#import "OMMallCategoryCollectCell.h"
#import "OMPageControl.h"

#define kMargin_top SM_SIZE(0)
#define kMargin_bottom kMargin_top
@interface OMMallCategoryTableCell () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, strong) OMPageControl *pageControl;
@property (nonatomic, strong) NSArray <OMMallCategoryItem*>*items;
@end


@implementation OMMallCategoryTableCell

- (void)configure
{
    [super configure];

    NSArray *array = @[
               @{@"name":@"我常买",@"image":@"home_category_fancy_",@"controller":@"OMFancyListViewController"},
               @{@"name":@"热销排行",@"image":@"home_category_rank_",@"controller":@"OMSalesRankViewController"},
               @{@"name":@"粉丝专区",@"image":@"home_category_funs_",@"controller":@"BaseTableViewController"},
               @{@"name":@"分类",@"image":@"home_category_all_",@"controller":@"OMConsumerGoodsViewController"},
               ];

    _items = [OMMallCategoryItem mj_objectArrayWithKeyValuesArray:array];

    [self.collectView registerClass:[OMMallCategoryCollectCell class]
         forCellWithReuseIdentifier:[OMMallCategoryCollectCell cellIdentifier]];

    [self.contentView addSubview:self.collectView];
    [self.collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(@(kMargin_top));
        make.bottom.equalTo(@(kMargin_bottom));
    }];

    self.contentView.backgroundColor = [UIColor blueColor];
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@(.5));
        make.height.equalTo(@(1));
    }];

}


- (void)configureCellWithObj:(NSArray <OMMallCategoryItem *>*)items_;
{

    NSMutableArray *items = [NSMutableArray arrayWithCapacity:items_.count];

    NSUInteger numOfRow = 2; //一页所包含的行数
    NSUInteger numOfColumn = 5; //一页所包含的列表
    NSUInteger numOfItemAtOnePage = 10; //一页所有的元素个数

    for (NSUInteger i = 0; i < items_.count; i ++) {
        NSUInteger index = ((i%numOfItemAtOnePage)/numOfRow) + numOfItemAtOnePage*(i/numOfItemAtOnePage) + (i%numOfRow == 0 ? 0 : numOfColumn);
        id obj = items_[index];
        [items addObject:obj];
    }
    _items = items;
    [self.collectView reloadData];
}

#pragma mark - Getters 

- (UICollectionView *)collectView
{
    if (!_collectView) {
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                         collectionViewLayout:[self layout]];
        _collectView.delegate = self;
        _collectView.dataSource = self;
        _collectView.backgroundColor = SM_whiteColor;
    }
    return _collectView;
}

- (UICollectionViewLayout*)layout
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    float itemSize = OMMallCategoryCollectCell_size;
    flowLayout.itemSize = CGSizeMake(itemSize, itemSize*.8);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    return flowLayout;
}

#pragma mark - collect view delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMMallCategoryCollectCell *cell = [OMMallCategoryCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
    [cell configureCellWithObj:self.items[indexPath.item]];
    return cell;
}

+ (CGFloat)cellHeight
{
    return OMMallCategoryCollectCell_size*.9+kMargin_top*2;
}

@end
